package com.doradance.main.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.model.remote.bean.BannerListBean;
import com.doradance.util.CollectionUtils;
import com.doradance.view.recyclerview.ViewHolderModel;
import com.yong.android.framework.view.recyclerview.RecyclerViewCell;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/12/27.
 */
public class BannerCell extends RecyclerViewCell<BannerCell.BannerHolder> {
    private List<BannerListBean.ItemBean> list;
    ConvenientBanner conBanner;

    @Override
    public BannerHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        return new BannerHolder(inflater, parent);
    }

    @Override
    public void onBindViewHolder(BannerHolder holder, int position) {
        holder.bindModel(list);
        conBanner = holder.mConBanner;
        if(holder.mConBanner!=null){
            holder.mConBanner.setPages(new CBViewHolderCreator() {
                @Override
                public NetworkImageHolderView createHolder() {
                    return new NetworkImageHolderView();
                }
            },list). //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                setPageIndicator(new int[]{R.mipmap.black, R.mipmap.red})
                    //设置指示器位置（左、中、右）
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                    .setManualPageable(true);
        conBanner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mBannerItemClickListener.bannerItemClick(position);
            }
        });


        }

    }
    interface BannerItemClickListener{
        void bannerItemClick(int position);
    }
    private BannerItemClickListener mBannerItemClickListener;

    public void setBannerItemClickListener(BannerItemClickListener bannerItemClickListener){
            mBannerItemClickListener =bannerItemClickListener;
    }


    public void updateSource(List<BannerListBean.ItemBean> list) {
        if(conBanner!=null){
            conBanner.startTurning(4000);
        }
        this.list = list;
    }

    @Override
    public int getCount() {
        return CollectionUtils.listSize(list) == 0 ? 0 : 1;

    }

    //A、网络图片
    public class NetworkImageHolderView implements Holder<BannerListBean.ItemBean> {
        private ImageView imageView;
        @Override
        public View createView(Context context) {
            //你可以通过layout文件来创建，也可以像我一样用代码创建，不一定是Image，任何控件都可以进行翻页
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, BannerListBean.ItemBean data) {
            imageView.setImageResource(R.mipmap.ic_launcher);
            Glide.with(context).load(data.getImage()).placeholder(R.mipmap.ic_launcher).into(imageView);
        }


    }

    static class BannerHolder extends ViewHolderModel<List<BannerListBean.ItemBean>> {

        Context mContext;
        @Bind(R.id.conBanner)
        ConvenientBanner mConBanner;


        public BannerHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_banner_home, parent, false));
            mContext = parent.getContext();
        }

        @Override
        public void bindModel(List<BannerListBean.ItemBean> itemBeans) {

        }



        @Override
        protected void bindView() {
            ButterKnife.bind(this, itemView);
            super.bindView();
        }
    }




}
